package com.zrrd.order.service;

import com.zrrd.order.mapper.OrderMapper;
import com.zrrd.order.pojo.Order;
import com.zrrd.order.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Resource
    private RestTemplate restTemplate;

    public Order queryOrderById(Long orderId) {
        //1.查询订单
        Order order = orderMapper.findById(orderId);
        //2、修改order-service服务中的OrderService类中的queryOrderById方法，根据Order对象中的userId查询User
        //2.1编写url地址
        //String url="http://localhost:8082/user/"+order.getUserId();
        //使用服务名称替换主机IP+端口号
        String url="http://userservice/user/"+order.getUserId();
        //2.2发送调用请求
        User user = restTemplate.getForObject(url, User.class);
        //3、存入到order
        order.setUser(user);
        //4、返回结果
        return order;
    }

}
